import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import EmployeeUser from '../views/EmployeeUser.vue'
import DrinkInfo from '../views/DrinkInfo.vue'
import EmployeeRole from '../views/EmployeeRole.vue'
import Liquorstock from '../views/Liquorstock.vue'
const routes = [
  {
    path: '/',
    name: 'LoginView',
    component: function () {
      return import(/* webpackChunkName: "about" */ '../views/LoginView.vue')
    }
  }, 
  {
    path: '/home',
    name: "home",
    component: HomeView,
    children: [
      {
        path: '/EmployeeUser',
        name: 'EmployeeUser',
        component: EmployeeUser,
      },
      {
        path: '/DrinkInfo',
        name: 'DrinkInfo',
        component: DrinkInfo
      },
      {
        path: '/EmployeeRole',
        name: 'EmployeeRole',
        component: EmployeeRole
      },
      {
        path: '/Liquorstock',
        name: 'Liquorstock',
        component: Liquorstock
      }
    ]
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: function () {
      return import('../views/AboutView.vue')
    }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    next();
  } else {
    let token = sessionStorage.getItem('token');
    //console.log(token)
    if ( token === '' || token===null) {
      next('/');
    } else {
      next();
    }
  }
});
export default router
